<?
/*
================ eBay Template API v1.0 ================
This class loads in the eBay Template from the
live template API, then caches it.

PRE-REQUISITES: PHP 5, mail, CURL,
chmod(755)ed caching directory.
========================================================
*/
class ebayTemplate extends errorHandler{
	#The URL to the eBay Template API.
	private $api="http://tools.ebay.com.au/template/";

	#The directory to store all the cached files. 
	public $cache_dir="cache/";

	#The number of hours to cache a version of the template. 
	public $caching=72;

	#The HTML Container
	public $html="";
	#The Opening HTML Container (i.e. will hold the <html> tag)
	public $openHtml="";
	#The Header HTML Container
	public $headHtml="";
	#The End Head/Open Body Tags
	public $tagsHtml="";
	#The Body HTML Container
	public $bodyHtml="";
	#The Footer HTML Container
	public $footHtml="";
	#The Closing HTML Container (i.e. will hold the </html> tag)
	public $closeHtml="";
	#
	public $__cacher=NULL;

	#Constructor
	public function __construct(){
		parent::__construct();
		$this->__cacher=new cacher();
	}
	
	#Load the template.
	public function load($params){
		$result=false;

		#Check the params are valid.
		if(substr($params,0,1)!="?"){
			$params="?$params";
		}

		#Check if we have a valid local version cached.
		$key=@md5($params).".tpl"; #The Cached File Name.
		
		#Check if the file is cached.
		if($result=$this->__cacher->load($key)){
		}elseif($result=$this->loadApiTemplate($params)){
			if($this->__cacher->cache($key,$result)){
			}else{
				$this->logError("Critical!!! Failed to cache the Template.");
			}
		}else{
			$this->logError("Critical!!! Failed to load in the API Template.");
		}
		
		//return($result);
		return($this->parse($result));
	}
	
	#Load in the template from the live API.
	public function loadApiTemplate($params){
		$url=$this->api.$params; 
		$ch=curl_init();
		@curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		@curl_setopt($ch,CURLOPT_URL,$url);
		$result=curl_exec($ch);
		@curl_close($ch);
		if(strlen($result)<1){
			$result=false;
		}
		return($result);
	}
	
	#Send an email to an eBay developer to look into why this error is occuring.
	public function logError($error){
		#$this->debug("Hmmm error",$error);
		$error=date("d/m/Y H:i:s")." - ERROR{$error} - URL{".$this->getFullUrl()."}\n";
		@file_put_contents($this->cache_dir."error_log",$error,FILE_APPEND);
		$subject="ebayTemplate.php ERROR!!";
		#@mail("tgeiger@ebay.de",$subject,$error);
		@mail("lsorhus@hotmail.com",$subject,$error);
		#@mail("pawood@ebay.com",$subject,$error);
	}
	
	#Get the full current URL.
	public function getFullUrl(){
		$s=empty($_SERVER["HTTPS"])?'':($_SERVER["HTTPS"]=="on")?"s":"";
		$protocol=substr(strtolower($_SERVER["SERVER_PROTOCOL"]),0,strpos(strtolower($_SERVER["SERVER_PROTOCOL"]),"/")).$s;
		$port=($_SERVER["SERVER_PORT"]=="80")?"":(":".$_SERVER["SERVER_PORT"]);
		return($protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']);
	}
	
	#Break up the Full Template into pieces.
	public function parse(){
		$result=false;
		if(strlen($this->html)>0){
			$tmp_ar="";
			$re="<!-- eBayTemplate::BP(::[^/]*)*//-->";
			if(eregi($re,$this->html)){
				$tmp_ar=split($re,$this->html);
				$this->openHtml=$tmp_ar[0];
				$this->headHtml=$tmp_ar[1];
				$this->tagsHtml=$tmp_ar[2];
				$this->bodyHtml=$tmp_ar[3];
				#NOTHING: $this->blank=$tmp_ar[4];
				$this->footHtml=$tmp_ar[5];
				$this->closeHtml=$tmp_ar[6];
				$result=true;
			}else{
				#echo("Not a valid eBayTemplate");
			}
		}else{
			#echo("Data not available.");
		}
		return($result);
	}
	
	#HTML GETS
	public function getOpen(){
		return($this->openHtml);
	}
	public function getHead(){
		return($this->headHtml);
	}
	public function getTags(){
		return($this->tagsHtml);
	}
	public function getBody(){
		return($this->bodyHtml);
	}
	public function getFoot(){
		return($this->footHtml);
	}
	public function getClose(){
		return($this->closeHtml);
	}
}
?>